﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainPanel : AppUI
{
    public int candidatesNum = 5;

    public ExplorePanel explorePanel;
    public CommunityUI communityPanel;
    public MsgPanel msgPanel;
    public MyPagePanel mePanel;

    public const int Panel_Explore = 1;
    public const int Panel_Community = 2;
    public const int Panel_Msg = 3;
    public const int Panel_Me = 4;

    int lastSelectToggle = -1;

    public ListTogglesGroup bottomTogglesGroup;
    // Start is called before the first frame update
    public override void Awake()
    {
        int defaultPanel = string.IsNullOrEmpty(MGame.instance.MUser.userInfo.locationCity) ? Panel_Me : Panel_Explore;

        if (bottomTogglesGroup)
            bottomTogglesGroup.Init(OnToggleSelect, defaultPanel);

        var game = MGame.instance;

    }

    public override void Start()
    {
        base.Start();

        communityPanel.PreloadData();
    }

    public override void DoOnActive()
    {
        base.DoOnActive();

        Refresh();
    }

    //public static 
  
    private void OnToggleSelect(int id)
    {
        if (id == Panel_Me)
        {
            bool baseInfoNotEdit = string.IsNullOrEmpty(MGame.instance.MUser.userInfo.locationCity);
            if (baseInfoNotEdit)
            {
                GameDebug.Log($"OnToggleSelect id:{id} location:{MGame.instance.MUser.userInfo.locationCity}");
                lastSelectToggle = id;
                UIController.ShowUI<EditBaseInfoPanel>();
                return;
            }
        }

        if (lastSelectToggle == id) return;

        lastSelectToggle = id;
        Refresh();
    }

    public void Refresh()
    {
        if (bottomTogglesGroup)
            bottomTogglesGroup.UpdateToggle(lastSelectToggle);

        explorePanel.gameObject.SetActive(lastSelectToggle == Panel_Explore);
        communityPanel.gameObject.SetActive(lastSelectToggle == Panel_Community);
        msgPanel.gameObject.SetActive(lastSelectToggle == Panel_Msg);
        mePanel.gameObject.SetActive(lastSelectToggle == Panel_Me);

        if (lastSelectToggle == Panel_Explore)
        {
            RefreshExplore();
        }
        else if (lastSelectToggle == Panel_Community)
        {
            //GameDebug.Log("main panel RefreshMoments");
            RefreshMoments();
        }
        else if (lastSelectToggle == Panel_Msg)
        {
            RefreshMsg();
        }
        else if (lastSelectToggle == Panel_Me)
        {
            RefreshMyPage();
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
    void RefreshExplore()
    {
        RequestServerController.instance.ReqExploreData(candidatesNum, (exploreData) =>
        {
            if (exploreData != null)
            {
                explorePanel.Init(exploreData);
            }
            else
            {
                if(this && gameObject)
                    DoActionDelay.DelayAction(5, RefreshExplore);
            }
        });
    }
    void RefreshMoments()
    {
        communityPanel.Init();
    }
    internal void RefreshMsg()
    {
        MsgPanelData msgPanelData = new MsgPanelData();
        msgPanelData.chats = MGame.instance.MUser.chats;
        msgPanelData.applications = MGame.instance.MUser.applies;
        //GameDebug.LogError($"applications count:{msgPanelData.applications.Count}");
        msgPanel.Init(msgPanelData);
    }
    void RefreshMyPage()
    {
        mePanel.Init();
    }
}
